package com.czk.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.czk.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000120606345";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCK06OEpnk6ihHnRSdlilA0s9xWOY+GAh8Ro+aAl+bpksUk4ZfsNYqTzL8bgTu1x+T7HQQmy6341s0Epxw1za6tWJzyuIHhOmfQFgwIxjPGUkpixADqCfTqIhsIORBaXN6czEXaSD+wJ9ggEnQFyXN60V4EaS/lGlhTVQUDgxuKFSrzcBwZFtiEDrCxMTdHQgsA/iYYdb1f2cfPeRhdye2XR+4e4Ip9S7EboX2fRFZ/mPnJzqHwKYKMmK0NeXK1ePSAt7d8lg5uncGU5ifGw6hHQ+ueIQresY4T7NRlHEYrNLzvx/CW9fz84liVI6/bFl2iRVyG40YhzRf5AY+MWNIHAgMBAAECggEBAInCU+wI6hlt8zAkjmCTbN2vcUDa45IePshZdR08Hza+nwTnG8OYqcfaj/VyO1c6Yf3RoFIDIcLhHBa24SZ+0QxQ94VgE/n05Bk4F38Jd00gUZJKX4HemXtQlrSFM7YHnBvKFLLzqMdb8np49UrcycwhQINuAcn9Z5JhtpTyn/xXXieYfvXl6mDUWH/QdbJw0KPaEgsdQ1S7vJZUIBlE1Vos8blVNizzJZc6ukG6oUhxLUXVYAy2diFtVAnupJwziWLhf5QgWQGx3VneKx/2PHr+oeOlcgwRo5IjSO2jheo/IXl1nyDTLL5XuhuTBO2OafPlGPGY+Qo3RUaCZmAN6RECgYEA713lj/iXuSrW+2QdMkb6KTwhci6ZYj47RzDRcgSQgw3BZQC3UpgY3mzpBu9Adt8PYaQFJUdRV53IETXgJCOFqnEZXjPQEMBIAZiUoLAS939i7KfIZm8rQtUtFw0pzoMf9f9BqL76u/8oAFbI/5jnVQWwc5462serfhXSNlemIr8CgYEAlHk7Rc7v/tmeRX47pFd1qaPpP6tGE2izGMOHY4iMeBorcTv003Gqf0h6QPgCcc6vz4WUDcnpoWays50Lk4yjtRuH3qg0ktNWlZRDjqVov/yGUuwJDFowsGe8ar+88vKPEYBv82GUTycJCWkoqng75F4PAvsw6umUJHOpjGzhyrkCgYBkUfsjcvHL5luBsvzVlI/F9nCiFUuttB1OAfTWD3mto3c5aKpskFhj6Etl36Ip7+TXbFHHxPMfgH5YUDcnjke4HnyeUaLTUnMbUCKCkijw18/TFaLU46TC9utV7OtnpOchR6DyI1CcuBzAv56wGRpjNOY2HsvV47dJOcYHhoaXqwKBgQCUZ0CpnBgioZ4jU/OFZq4DLATeUPGwQLyTCg1vnXLaMT4u5oDmP/ztUKJyAfNfkfoE8T11MtbFLwN7qIODrdWMFsKN+Y7jGz62MAX+tLKpA1230JmSsStth6K7CH9yTqRWGAzONkCphRjxQR0Wlys2LhXPIwhYJbSfYsSbydE+cQKBgCesNlFsfvqgY28IoBuJ+L+r6ScEVM1k2KtNl2yWR/Ictks6p7EXdLFrRzfHQAAcaps0Q0xohm7usTEQp6zYcBW4O/UW3sVSPoqQbriErBVREnRtol2q+Igqw/WdbZReUz7J8X1f91FIo41gDnZ++VWy8ntQTM4axs6jyq2bEmHq";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAveBFzs2TV20MlaWh4L00aMi6UyK6sx33htbzK2cYjMET07MCsBrDZ+xGkur2VinyccfdTehqoKwmSkVNkUL2cqA3LEw6QsfgWB4GSCY57y0usHJE4jP+hZhVI4H08zAjg9iCm00AKRxM3HJ9sWSs0u6zGz/0t7c3+j0qQfqLwEP4hPTueMH1Vc+02I16BrfztLYTm+bA6XQfwEtuhBRJNYzQNJ26gocH9akqBchFfVRstRGSfG1ix/OQ6FCvL2qmzeZNGzQwscsfYlnd6AstVa+A/MwZ94f9AwjBHeZb0Va1dTM94tT4m4Hw1VL7ZsBtSyeZhFTMBRaRdcRjOoQj8wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://nzq.free.idcfengye.com/psyrf/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"1m"+","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
